package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.GonggaoBean;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class AnnouncementActivity extends BaseActivity {

    @BindView(R.id.back_announcement)
    ImageView backAnnouncement;
    @BindView(R.id.recycel_announcement)
    RecyclerView recycelAnnouncement;
    @BindView(R.id.refresh_announcent)
    SmartRefreshLayout refreshAnnouncent;
    @BindView(R.id.zanwushujuzhannei)
    LinearLayout zanwushujuzhannei;
    private BaseQuickAdapter mAdapter;
    private int page = 1;
    private List<GonggaoBean.DataBean.ListBean> datas = new ArrayList<>();
    private GonggaoBean gong;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announcement);
        ButterKnife.bind(this);
        intView();
        intData();
    }

    private void intData() {
        OkHttpUtils.get(HttpManager.IP + "/sys-getNotice")
                .params("page", String.valueOf(page))
                .params("menuId", "2")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        refreshAnnouncent.finishRefresh();
                        refreshAnnouncent.finishLoadMore();
                        Gson g = new Gson();
                        try {
                            gong = g.fromJson(s, GonggaoBean.class);
                            if (gong.getReturnCode() == 200) {
                                datas.addAll(gong.getData().getList());

                                mAdapter.notifyDataSetChanged();
                            }
                            if (datas.size()<=0){
                                zanwushujuzhannei.setVisibility(View.VISIBLE);
                            }else {
                                zanwushujuzhannei.setVisibility(View.GONE);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void intView() {
        recycelAnnouncement.setAdapter(mAdapter = new BaseQuickAdapter<GonggaoBean.DataBean.ListBean, BaseViewHolder>(R.layout.gonggao_item, datas) {
            @Override
            protected void convert(BaseViewHolder helper, GonggaoBean.DataBean.ListBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.gonggaobiaoti, item.getTitle());
                Glide.with(mContext).load(item.getMinimg()).into((ImageView) helper.getView(R.id.image_gonggao));
            }
        });
        recycelAnnouncement.setLayoutManager(new LinearLayoutManager(this));
        refreshAnnouncent.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                intData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                datas.clear();
                intData();
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                intent = new Intent(AnnouncementActivity.this, AnnouncementallActivity.class);
                intent.putExtra("maximage", datas.get(position).getMaximg());
                startActivity(intent);

            }
        });
    }

    @OnClick(R.id.back_announcement)
    public void onViewClicked() {
        goBack();
    }
}
